package com.ivo.quartz;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.ivo.biz.PerformanceMon;
import com.ivo.biz.PerformanceMonManager;
import com.ivo.biz.PerformanceService;
import com.ivo.core.hibernate.HibernateInvoker;
import com.ivo.sys.Code;
import com.ivo.sys.CodeService;

/**
 * 
 *
 * @author Minkey Wang
 * @version 2017年6月7日
 */
@Repository
public class AttendanceQuartz {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private PerformanceService performanceService;
	
    @Autowired
	private HibernateInvoker hibernateInvoker;
    
	@Autowired
	private PerformanceMonManager performanceMonManager;
	
	@Autowired
	private CodeService codeService;
	
	
	@Transactional
    @Scheduled(cron = "0 0 5,6 16 * ? ")//每月16日上午05:00、06:00触发
	public void execute() {
		//同步考勤数据
		Calendar calendar = Calendar.getInstance(); 
//		calendar.add(Calendar.MONTH, -1);    //得到前一个月   
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(calendar.getTime());
		String month = str.substring(0, 7);
		
//		String month ="2017-09";
	    RestTemplate restTemplate = new RestTemplate();
	    Gson googleJson = new Gson();
	    JsonParser parser = new JsonParser();
	    
	   logger.info("同步考勤数据start");
	    String url = "http://myivo.ivo.com.cn/hrReport/restful/getAttRecord.do?month="+month;//http://myivo.ivo.com.cn/hrReport/restful/getAttRecord.do?month=2017-05
	    String json = restTemplate.getForObject(url, String.class);  
	    JsonArray Jarray   = parser.parse(json).getAsJsonArray();
	    List<Attendance> jsonObjList =  googleJson.fromJson(Jarray, new TypeToken<List<Attendance>>() { }.getType());
	    for(int i =0; i<jsonObjList.size();i++){
	    	Attendance attendance = jsonObjList.get(i);
	    	PerformanceMon perTemp = performanceService.getPerformanceMon(month,attendance.getEmployee_ID());
	    	if(perTemp==null){
	    		PerformanceMon performanceMon = new PerformanceMon();
	    		performanceMon.setMonth(month);
	    		performanceMon.setFactory(attendance.getDeptName_S());
	    		if(attendance.getDeptName_S().equals("LCM1")){
	    			performanceMon.setFactory("M1");
	    		}else if(attendance.getDeptName_S().equals("LCM2")){
	    			performanceMon.setFactory("M2");	
	    		}else if(attendance.getDeptName_S().equals("WH")){
	    			if(attendance.getDepartment_FK().equals("10000434")){
	    				performanceMon.setDepartment_name("材料仓储课");
	    				performanceMon.setDepartment_id(attendance.getDepartment_FK());
	    			}else if(attendance.getDepartment_FK().equals("10000380")){
	    				performanceMon.setDepartment_name("模一仓储课");
	    				performanceMon.setDepartment_id(attendance.getDepartment_FK());
	    			}else if(attendance.getDepartment_FK().equals("10000379")){
	    				performanceMon.setDepartment_name("成品仓储课");
	    				performanceMon.setDepartment_id(attendance.getDepartment_FK());
	    			} 
	    		}
	    			
	    		
	    			
	    		performanceMon.setEmp_id(attendance.getEmployee_ID());
	    		performanceMon.setEmp_name(attendance.getEmployeeName());
	    		performanceMon.setGender(attendance.getGender());
	    		if("0".equals(attendance.getAbscFlag()) && !"WH".equals(attendance.getDeptName_S())){
	    			performanceMon.setAttdHour(0.0);
	    		}else{
	    			performanceMon.setAttdHour(attendance.getAttdHour());
	    		}
	    		performanceMon.setShftHour(attendance.getShftHour());
	    		performanceMon.setPrsnHour(attendance.getPrsnLeave());
	    		performanceMon.setSickHour(attendance.getSickLeave());
	    		performanceMon.setLeaveHour(attendance.getLeaveHour());
	    		performanceMon.setDateOfRegister(attendance.getDateOfRegister());
	    		performanceMonManager.save(performanceMon);
	    	}else {
	    		
	    		perTemp.setFactory(attendance.getDeptName_S());
	    		if(attendance.getDeptName_S().equals("LCM1")){
	    			perTemp.setFactory("M1");
	    		}else if(attendance.getDeptName_S().equals("LCM2")){
	    			perTemp.setFactory("M2");	
	    		}else if(attendance.getDeptName_S().equals("WH")){
	    			if(attendance.getDepartment_FK().equals("10000434")){
	    				perTemp.setDepartment_name("材料仓储课");
	    				perTemp.setDepartment_id(attendance.getDepartment_FK());
	    			}else if(attendance.getDepartment_FK().equals("10000380")){
	    				perTemp.setDepartment_name("模一仓储课");
	    				perTemp.setDepartment_id(attendance.getDepartment_FK());
	    			}else if(attendance.getDepartment_FK().equals("10000379")){
	    				perTemp.setDepartment_name("成品仓储课");
	    				perTemp.setDepartment_id(attendance.getDepartment_FK());
	    			} 
	    		}
	    		perTemp.setMonth(month);
	    		perTemp.setEmp_name(attendance.getEmployeeName());
	    		perTemp.setGender(attendance.getGender());
	    		perTemp.setAttdHour(attendance.getAttdHour());
	    		perTemp.setShftHour(attendance.getShftHour());
	    		perTemp.setPrsnHour(attendance.getPrsnLeave());
	    		perTemp.setSickHour(attendance.getSickLeave());
	    		perTemp.setLeaveHour(attendance.getLeaveHour());
	    		perTemp.setDateOfRegister(attendance.getDateOfRegister());
	    		performanceMonManager.save(perTemp);
	    	}

	    }
	    logger.info("同步考勤数据end");
	    
	    //同步fhr人员基本信息
	    logger.info("同步fhr人员基本信息start");
		String fhrurl = "http://myivo.ivo.com.cn/FHR/restful/getDetailsOfEmp.do";
		String fhrjson = restTemplate.getForObject(fhrurl, String.class);
		JsonArray fhrJarray   = parser.parse(fhrjson).getAsJsonArray();
		List<Operation> jsonFhrList =  googleJson.fromJson(fhrJarray, new TypeToken<List<Operation>>() { }.getType());
		for(Operation operation:jsonFhrList){
			PerformanceMon perTemp = performanceService.getPerformanceMon(month,operation.getUser_id());
			if(perTemp!=null){
				Code code= codeService.getCode(operation.getOperation_id());
				if(code!=null ){
					Long id =code.getId(); 
					perTemp.setOpe_fk(id);
				}
				perTemp.setDepartment_id(operation.getDepartment_id());
				perTemp.setDepartment_name(operation.getDeptName_s());
				performanceMonManager.save(perTemp);
			}
		}
		logger.info("同步fhr人员基本信息end"); 
	    
	    
		logger.info("同步工种start");
		String url1 = "http://myivo.ivo.com.cn/FHR/restful/getTypeOfWork.do";
		String empId = "";
		String workType = "";
		String json1 = restTemplate.getForObject(url1, String.class);  
		JsonArray Jarray1 = parser.parse(json1).getAsJsonArray();
		for(int i =0; i<Jarray1.size();i++){
			empId = Jarray1.get(i).getAsJsonObject().get("employee_id").getAsString();
			workType = Jarray1.get(i).getAsJsonObject().get("typeOfWork").getAsString();
			PerformanceMon p = hibernateInvoker.findUnique(PerformanceMon.class, "from PerformanceMon p where p.emp_id = '" + empId + "'and p.month = '" + month + "'");
			if(p != null){
				Code c = hibernateInvoker.findUnique(Code.class, "from Code c where c.name = '" + workType + "'");
				if(c != null){
					p.setWorkType(c.getId());
					p.setWorkAbility(30.0);
					p.setCompatibility(30.0);
					performanceMonManager.save(p);
				}
			}
		}
		logger.info("同步工种end");
		
		//同步员工实时班别
		logger.info("同步员工班别start");
		String classurl = "http://myivo.ivo.com.cn/FHR/restful/getAllEmpBatch.do";
		String classjson = restTemplate.getForObject(classurl, String.class);
		JsonArray classJarray   = parser.parse(classjson).getAsJsonArray();
		List<Operation> jsonClassList =  googleJson.fromJson(classJarray, new TypeToken<List<Operation>>() { }.getType());
		for(Operation operation:jsonClassList){
			PerformanceMon performanceMon = performanceService.getPerformanceMon(month,operation.getUser_id());
	    	if(performanceMon!=null){
//		    		System.out.println(performanceMon.getEmp_id());
	    		performanceMon.setClassType(operation.getClassType());
	    		performanceMonManager.save(performanceMon);
	    	}
		}
		logger.info("同步员工班别end");
	}

}
